package com.bysc.userservice.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bysc.baseservice.handler.BaiYunException;
import com.bysc.commonutils.ResultCode;
import com.bysc.commonutils.Results;
import com.bysc.commonutils.constant.MessageConstant;
import com.bysc.commonutils.constant.RedisConstant;
import com.bysc.userservice.entity.UserMajor;
import com.bysc.userservice.entity.vo.MajorQueryVo;
import com.bysc.userservice.service.UserMajorService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author kkk
 * @since 2024-05-20
 */
@Api(tags = "专业管理")
@RestController
@RequestMapping("/userservice/user-major")
public class UserMajorController {
    @Autowired
    private UserMajorService userMajorService;

    @ApiOperation(value = "根据学院id新增专业")
    @CacheEvict(value = "index", key = "'CollegeAndMajor'", allEntries = true)
    @PostMapping("addMajorByCollegeId/{id}")
    public Results addMajorByCollegeId(@ApiParam(name = "id", value = "学院id", required = true) @PathVariable String id, @ApiParam(name = "MajorInfo", value = "专业信息", required = true) @RequestBody UserMajor userMajor) {
        try {
            userMajor.setCollegeId(id);
            userMajorService.save(userMajor);
            return Results.ok().message(MessageConstant.ADD_MAJOR_SUCCESS);
        } catch (Exception e) {
            throw new BaiYunException(ResultCode.ERROR, MessageConstant.ADD_MAJOR_FAIL);
        }
    }

    @ApiOperation(value = "根据专业id删除专业")
    @CacheEvict(value = "index", key = "'CollegeAndMajor'", allEntries = true)
    @DeleteMapping("deleteMajorById/{id}")
    public Results deleteMajorById(@ApiParam(name = "id", value = "专业id", required = true) @PathVariable String id) {
        try {
            UserMajor major = userMajorService.getById(id);
            if (major != null) {
                userMajorService.removeById(id);
            }
            return Results.ok().message(MessageConstant.DELETE_MAJOR_SUCCESS);
        } catch (Exception e) {
            throw new BaiYunException(ResultCode.ERROR, MessageConstant.DELETE_MAJOR_FAIL);
        }
    }

    @ApiOperation(value = "根据专业id修改专业信息")
    @CacheEvict(value = "index", key = "'CollegeAndMajor'", allEntries = true)
    @PutMapping("updateMajorById/{id}")
    public Results updateMajorById(@ApiParam(name = "MajorInfo", value = "专业信息", required = true) @RequestBody UserMajor userMajor) {
        try {
            userMajorService.updateById(userMajor);
            return Results.ok().message(MessageConstant.EDIT_MAJOR_SUCCESS);
        } catch (Exception e) {
            throw new BaiYunException(ResultCode.ERROR, MessageConstant.EDIT_MAJOR_FAIL);
        }
    }

    @ApiOperation(value = "根据专业id查找专业信息")
    @GetMapping("getMajorById/{id}")
    public Results getMajorById(@ApiParam(name = "id", value = "专业id", required = true) @PathVariable String id) {
        try {
            UserMajor major = userMajorService.getById(id);
            return Results.ok().data("major", major).message(MessageConstant.QUERY_MAJOR_SUCCESS);
        } catch (Exception e) {
            throw new BaiYunException(ResultCode.ERROR, MessageConstant.QUERY_MAJOR_FAIL);
        }
    }

    @ApiOperation(value = "根据学院id查找专业列表")
    @GetMapping("getMajorListByCollegeId/{id}")
    public Results getMajorListByCollegeId(@ApiParam(name = "id", value = "学院id", required = true) @PathVariable String id) {
        try {
            List<UserMajor> majorList = userMajorService.list(new QueryWrapper<UserMajor>().eq("college_id", id).orderByDesc("sort"));
            return Results.ok().data("majorList", majorList).message(MessageConstant.QUERY_MAJOR_SUCCESS);
        } catch (Exception e) {
            throw new BaiYunException(ResultCode.ERROR, MessageConstant.QUERY_MAJOR_FAIL);
        }
    }

    @ApiOperation(value = "带条件分页查询专业")
    @PostMapping("getMajorByQueryVo/{page}/{limit}")
    public Results getMajorByQueryVo(@ApiParam(name = "page", value = "查询页数", required = true)
                                     @PathVariable Integer page, @ApiParam(name = "limit", value = "查询数量", required = true)
                                     @PathVariable Integer limit, @ApiParam(name = "queryVo", value = "查询条件", required = false)
                                     @RequestBody MajorQueryVo queryVo) {
        try {
            Page<UserMajor> infoPage = new Page<>(page, limit);
            String name = queryVo.getName();
            String collegeId = queryVo.getCollegeId();

            LambdaQueryWrapper<UserMajor> wrapper = new LambdaQueryWrapper<>();
            if (!StringUtils.isEmpty(collegeId)) {
                wrapper.eq(UserMajor::getCollegeId, collegeId);
            }
            if (!StringUtils.isEmpty(name)) {
                wrapper.like(UserMajor::getName, name);
            }
            wrapper.orderByDesc(UserMajor::getSort);
            IPage<UserMajor> iPage = userMajorService.page(infoPage, wrapper);
            return Results.ok().data("list", iPage).message(MessageConstant.QUERY_MAJOR_SUCCESS);
        } catch (Exception e) {
            throw new BaiYunException(ResultCode.ERROR, MessageConstant.QUERY_MAJOR_FAIL);
        }
    }
}

